package com.cyan.app.workflow.activity.WHM;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.BaseBean;
import com.cyan.app.workflow.bean.Invbat2LabBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.ClickUtil;
import com.cyan.app.workflow.util.network.CommHttpListener;
import com.cyan.app.workflow.util.network.VolleyUtil;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 区域库位调拨-提交
 * Created by DXW on 2016-07-07.
 */
public class AreaDbSubActivity extends BaseActivity {
    public static final String TAG = "AreaDbSubActivity";
    private ArrayList<Invbat2LabBean.Invbat2Bean> mInvbat2BeanList = null ;
    private Button mButtonSubmit;
    private EditText mEditTextKw;
    private EditText mEditTextYg;
    private Button mButtonKw;
    private Button mButtonYg;
    private static final String YGBH = "area_db_ygbh";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.whm_area_db_sub);
        initView();
        //获取调拨列表
        mInvbat2BeanList= (ArrayList<Invbat2LabBean.Invbat2Bean>)getIntent().getSerializableExtra("area_db_db");
        mEditTextKw = (EditText)findViewById(R.id.whm_area_db_sub_kw);
        mEditTextYg = (EditText)findViewById(R.id.whm_area_db_sub_ygk);
        mButtonSubmit=(Button)findViewById(R.id.whm_area_db_sub_submit);
        mButtonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ClickUtil.isFastClick(20)) {
                    //20秒内不能重复操作
                    return;
                }
                if (mEditTextKw.getText().toString().isEmpty()) {
                    AppUtils.showTip(AreaDbSubActivity.this, "库位编号不能为空。");
                } else if (mEditTextYg.getText().toString().isEmpty()) {
                    AppUtils.showTip(AreaDbSubActivity.this, "员工编号不能为空。");
                } else if (mInvbat2BeanList == null || mInvbat2BeanList.size() == 0) {
                    AppUtils.showTip(AreaDbSubActivity.this, "没有可调拨的记录。");
                }else {
                    execDB();
                }
            }
        });
        mButtonKw =(Button)findViewById(R.id.whm_area_db_sub_kw_sm);
        mButtonKw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(AreaDbSubActivity.this, CaptureActivity.class), 0x0001);
            }
        });
        mButtonYg =(Button)findViewById(R.id.whm_area_db_sub_ygk_sm);
        mButtonYg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(AreaDbSubActivity.this, CaptureActivity.class), 0x0002);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0001 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("10010")) {
                AppUtils.showTip(AreaDbSubActivity.this, "库位不存在");
            }else{
                mEditTextKw.setText(String.valueOf(result.substring(9)));
            }
        }
        if(requestCode == 0x0002 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(AreaDbSubActivity.this, "员工不存在");
            }else {
                mEditTextYg.setText(String.valueOf(result.substring(5)));
                PreferenceManager.getDefaultSharedPreferences(this).edit().putString(YGBH,result.substring(5)).commit();
            }
        }
    }
    //初始化
    private void initView(){
        titleBar();
        //库位禁止键盘输入
        mEditTextKw = (EditText)findViewById(R.id.whm_area_db_sub_kw);
        mEditTextKw.setInputType(InputType.TYPE_NULL);
        //员工禁止键盘输入
        mEditTextYg = (EditText)findViewById(R.id.whm_area_db_sub_ygk);
        mEditTextYg.setInputType(InputType.TYPE_NULL);
        String ygbh = PreferenceManager.getDefaultSharedPreferences(this).getString(YGBH,null);
        if(ygbh!=null){
            mEditTextYg.setText(String.valueOf(ygbh));
        }
    }
    //初始化表头
    private void titleBar(){
        TextView mTextViewTitle =(TextView)findViewById(R.id.top_title_info);
        mTextViewTitle.setText("库位调拨提交");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }

    //执行调拨
    private void execDB() {
        String result = null;
        try {
            Map<String, String> params = new HashMap<String, String>();
            JSONArray array = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            for(Invbat2LabBean.Invbat2Bean c:mInvbat2BeanList){
                JSONObject json = new JSONObject();
                json.put("Itnbr", c.getItnbr());
                json.put("FromFixnr", c.getFixnr());
                json.put("Onhand1", c.getOnhand1());
                json.put("Onhand2", "0");
                json.put("ToFixnr", mEditTextKw.getText());
                //json.put("ToFixnr", "50");
                array.put(json);
            }
            jsonObject.put("Userno",mEditTextYg.getText());
            //jsonObject.put("Userno","1019");
            jsonObject.put("OperationType","310");
            jsonObject.put("ItnbrDetails",array);
            params.put("action", "315");
            params.put("params", jsonObject.toString());

            VolleyUtil.sendStringRequestByGet(AreaDbSubActivity.this, HttpConfig.HTTP_MAIN_SERVER_URL, TAG, params, null, new CommHttpListener<Object>() {
                @Override
                public void onResponse(String result) {
                    super.onResponse(result);
                    if (result == null) {
                        return;
                    }
                    BaseBean resultBean = new Gson().fromJson(result, BaseBean.class);

                    if (resultBean.getCode() == 1) {
                        AppUtils.showTip(AreaDbSubActivity.this, "调拨成功");
                        setResult(RESULT_OK);
                    } else {
                        AppUtils.showTip(AreaDbSubActivity.this, resultBean.getMsg());
                        setResult(RESULT_CANCELED);
                    }
                    finish();
                }

                @Override
                public void onCodeError(int code, String msg) {
                    AppUtils.showTip(AreaDbSubActivity.this, msg);
                }

                @Override
                public void onVolleyError(VolleyError volleyError) {
                    AppUtils.showTip(AreaDbSubActivity.this, R.string.network_error);
                }

                @Override
                public void onException(Exception e) {
                    AppUtils.showTip(AreaDbSubActivity.this, e.toString());
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            AppUtils.showTip(AreaDbSubActivity.this, "执行失败，请联系IT人员");
        }
    }
    class ItnbrDbResultBean extends BaseBean
    {
        private String Itnbr;
        private String Fixnr;
        private Double Onhand1;

        public String getItnbr() {
            return Itnbr;
        }

        public void setItnbr(String itnbr) {
            Itnbr = itnbr;
        }

        public String getFixnr() {
            return Fixnr;
        }

        public void setFixnr(String fixnr) {
            Fixnr = fixnr;
        }

        public Double getOnhand1() {
            return Onhand1;
        }

        public void setOnhand1(Double onhand1) {
            Onhand1 = onhand1;
        }
    }
}
